import 'package:flutter/material.dart';
import 'dart:io';
// import 'dart:convert';
// import 'package:json_annotation.dart';

var httpClient = new HttpClient();
class ExampleWidget extends StatefulWidget {
  ExampleWidget({Key key}) : super(key: key);

  @override
  _ExampleWidgetState createState() => new _ExampleWidgetState();
}

/// State for [ExampleWidget] widgets.
class _ExampleWidgetState extends State<ExampleWidget> {
  final TextEditingController _controller = new TextEditingController();
  final TextEditingController  _passController = new TextEditingController();
  bool showLoading = false;

  void login() async {
    var httpClient = new HttpClient();
    var uri = new Uri.http(
        'https://t.admin.yudada.com',
        '/api/auth',
        {'loginName': _controller.text, 'password': _passController.text});
    var request = await httpClient.getUrl(uri);
    var response = await request.close();
    // var responseBody = await response.transform(UTF8.decoder).join();
    // var data = JSON.decode(responseBody);
    // print(data);
    print(response);
  }

  void _loginCheck() {
    String error = '';
    if (_passController.text.length == 0){
      error = '请输入密码~';
    }

    if (_controller.text.length != 11){
      error = '请输入正确的手机号码~';
    }

    if (error.length != 0){
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text(error),
        )
      );
      return;
    }

    setState(() {
      showLoading = !showLoading;
    });
    login();
  }

  @override
  Widget build(BuildContext context) {
    return new SizedBox(
      width: 300.0,
      height: null,
      child: new Column(
        children: <Widget>[
          new TextField(
            controller: _controller,
            textDirection: TextDirection.rtl,
            keyboardType: TextInputType.phone,
            maxLength: 11,
            textAlign: TextAlign.left,
            decoration: new InputDecoration(
              contentPadding: EdgeInsets.all(5.0),
              icon: Icon(Icons.people),
              labelText: '请输入你的用户名',
            ),
            onChanged: (text) {
              print(text);
            },
          ),
          new TextField(
            controller: _passController,
            textDirection: TextDirection.rtl,
            keyboardType: TextInputType.phone,
            obscureText: true,
            maxLength: 15,
            textAlign: TextAlign.left,
            decoration: new InputDecoration(
              // contentPadding: EdgeInsets.all(5.0),
              icon: Icon(Icons.phone_android),
              labelText: '请输入你的密码',
              // helperText: '请输入你的真实姓名'
            ),
            onChanged: (text) {
              print(text);
            },
          ),
          RaisedButton(
            onPressed: _loginCheck,
            child: this.showLoading ? new Center(child: new CircularProgressIndicator(
             valueColor: AlwaysStoppedAnimation(Colors.blue),
             strokeWidth: 3
            ),
            heightFactor: 1.5,
            widthFactor: 4.0,) : Text('登录'),
          ),
        ]
      )
    );
  }
}
